package com.example.fengyungo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fengyungo.R;
import com.example.fengyungo.activity.message.ChatActivity;
import com.example.fengyungo.constant.MessageConstant;
import com.example.fengyungo.domain.MessageList;
import com.example.fengyungo.utils.BitmapUtils;

import java.util.List;

public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.ViewHolder> {

    private static final String TAG = "MessageListAdapter";

    protected List<MessageList> mMessageListList;

    protected Context mContext;

    /**
     * 局部刷新item数据
     *
     * @param position 位置
     */
    public void refreshPartItem(int position) {
        Log.d(TAG, "refreshPartItem: position=" + position);
        if (position > mMessageListList.size() - 1) {
            Log.d(TAG, "refreshPartItem: position > mMessageListList.size() - 1");
            return;
        }
        // 局部刷新的主要api，参数一：更新的item位置，参数二：item中被标记的某个数据
        // changePos 为0：数据1   为1：数据2
        notifyItemChanged(position, 0);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView userNickname;
        TextView lastContent;
        TextView lastTime;
        TextView driverUnread;
        // TODO: 2023/12/13 头像的设置
        ImageView avatar;
        View orderView;

        public ViewHolder(View view) {
            super(view);
            orderView = view;
            userNickname = view.findViewById(R.id.name);
            lastContent = view.findViewById(R.id.content);
            lastTime = view.findViewById(R.id.send_time);
            driverUnread = view.findViewById(R.id.unread);
            avatar = view.findViewById(R.id.avatar);
        }
    }

    public MessageListAdapter(List<MessageList> messagelistlist) {
        mMessageListList = messagelistlist;
    }

    /**
     * 用于创建ViewHolder实例
     *
     * @param parent   ViewGroup
     * @param viewType ViewHolder
     * @return int
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (mContext == null) {
            mContext = parent.getContext();
        }

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_message_list, parent, false);
        ViewHolder holder = new ViewHolder(view);

        holder.orderView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getLayoutPosition();
                MessageList messageList = mMessageListList.get(position);
                Intent intent = new Intent(mContext, ChatActivity.class);
                intent.putExtra(MessageConstant.USER_ID, messageList.getUserId());
                intent.putExtra(MessageConstant.DRIVER_UNREAD, messageList.getDriverUnread());
                intent.putExtra(MessageConstant.USER_NICKNAME, messageList.getUserNickname());
                mContext.startActivity(intent);
                // TODO: 2023/12/16 从ChatActivity返回到MessageFragment要能自动刷新消息列表
                // TODO: 2023/12/16 进入ChatActivity后将消息未读数更新为0
            }
        });

        return holder;
    }

    /**
     * 对RecyclerView子项的数据赋值
     *
     * @param holder   ViewHolder
     * @param position int
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MessageList messageList = mMessageListList.get(position);
        holder.userNickname.setText(messageList.getUserNickname());
        holder.lastContent.setText(messageList.getLastContent());
        holder.lastTime.setText(messageList.getLastTime());
        String avatarUrl = messageList.getUserAvatar();
        // 设置头像
        Bitmap bitmapFromSp = BitmapUtils.getBitmapFromSharedPreferences((FragmentActivity) mContext, messageList.getUserId());
        Log.d(TAG, "onBindViewHolder: userNickname=" + messageList.getUserNickname());
        Log.d(TAG, "onBindViewHolder: bitmapFromSp=" + bitmapFromSp);
        Log.d(TAG, "onBindViewHolder: avatarUrl:" + avatarUrl);
        BitmapDrawable avatarDrawable = new BitmapDrawable(mContext.getResources(), bitmapFromSp);
        Log.d(TAG, "onBindViewHolder: avatarDrawable=" + avatarDrawable);
        holder.avatar.setBackground(avatarDrawable);
        // 若未读消息不为0，显示未读消息数
        Log.d(TAG, "onBindViewHolder: messageList:" + messageList);
        if (messageList.getDriverUnread() == 0) {
            holder.driverUnread.setVisibility(View.INVISIBLE);
        } else {
            holder.driverUnread.setVisibility(View.VISIBLE);
            holder.driverUnread.setText(String.valueOf(messageList.getDriverUnread()));
        }
    }

    @Override
    public int getItemCount() {
        return mMessageListList.size();
    }
}

